

#include <sdk_mem.h>
#include <sudoku.h>

#include <stdlib.h>

unsigned int memory_usage;

sudoku *sdk_malloc_sdk(){

  return sdk_malloc(sizeof(sudoku));

}

sdk_square *sdk_malloc_sqr(){

  return sdk_malloc(sizeof(sdk_square));

}

/**
 * Frees a sudoku that has lists allocated to it.
 */
void sdk_free_sudoku(sudoku *sdk){

  if (!sdk) return;

  int i, j;

  for ( i = 0; i < SDK_WIDTH; i++){
    for ( j = 0; j < SDK_WIDTH; j++){
      if (sdk->sdk_data[i][j].pos != NULL){
	SDK_DEALLOC_LLIST(sdk->sdk_data[i][j].pos);
      }
    }
  }

  sdk_free(sdk, sizeof(sudoku));

}

void *sdk_malloc(unsigned int len){

  void *mem = malloc(len);
  if (mem){
    memory_usage += len;
    return mem;
  } else {
    return NULL;
  }

}

void sdk_free(void *ptr, unsigned int len){

  free(ptr);
  memory_usage -= len;

}

unsigned int sdk_memory_usage(){

  return memory_usage;

}
